package com.ceshiren.mini.service.impl;

import com.ceshiren.mini.converter.RecordConverter;
import com.ceshiren.mini.dao.RecordMapper;
import com.ceshiren.mini.dto.OperateJenkinsJobDTO;
import com.ceshiren.mini.dto.PlanTestCasesDTO;
import com.ceshiren.mini.dto.RecordDTO;
import com.ceshiren.mini.dto.ReportDTO;
import com.ceshiren.mini.entity.Plan;
import com.ceshiren.mini.entity.Record;
import com.ceshiren.mini.service.JenkinsService;
import com.ceshiren.mini.service.PlanService;
import com.ceshiren.mini.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.ceshiren.mini.common.Constants.STATUS_ONE;

@Service
public class RecordServiceImpl implements RecordService {
    private PlanService planService;
    private JenkinsService jenkinsService;

    private RecordConverter recordConverter;
    private RecordMapper recordMapper;
    @Autowired
    public void setPlanService(PlanService planService) {
        this.planService = planService;
    }

    @Autowired
    public void setRecordConverter(RecordConverter recordConverter) {
        this.recordConverter = recordConverter;
    }



    @Autowired
    public void setJenkinsService(JenkinsService jenkinsService) {
        this.jenkinsService = jenkinsService;
    }
    @Autowired
    public void setRecordMapper(RecordMapper recordMapper) {
        this.recordMapper = recordMapper;
    }

    //执行测试计划
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecordDTO run(RecordDTO recordDTO) {

        //Plan
        //测试计划ID
        Integer planId = recordDTO.getPlan_id();
        PlanTestCasesDTO planTestCasesDTO = new PlanTestCasesDTO();
        planTestCasesDTO.setId(planId);
        //1.查看当前测试计划是否存在
        Plan plan = planService.getPlan(planTestCasesDTO);

        if(Objects.isNull(plan))
            return null;

        //2.测试计划存在则直接调用Jenkins接口
        System.out.println("plan:"+plan);
        OperateJenkinsJobDTO operateJenkinsJobDTO = jenkinsService.runTask(plan);
        //创建测试执行条数 record
        System.out.println("jenkinsService - operateJenkinsJobDTO:" + operateJenkinsJobDTO);

        String jenkinsUrl = operateJenkinsJobDTO.getJenkinsUrl();
        String jobId = operateJenkinsJobDTO.getJobId().toString();

        Record record = new Record();
        record.setPlanId(planId);
        record.setJobId(jobId);
        record.setBuildUrl(jenkinsUrl);
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        record.setStatus(STATUS_ONE);
        //构建完成后才能获取allure报告
        //allure报告地址 http://127.0.0.1:8080/job/mini_job/ + 28 + /allure/
        //record.setReportUrl(jenkinsUrl+jobId+"/allure/");

        System.out.println("record:" + record);
        //数据库插入
        recordMapper.insertUseGeneratedKeys(record);
        //System.out.println(testTask);
        return recordConverter.recordForRecordDTO(record);

    }

    @Override
    public List<RecordDTO> getRecordDTOList(RecordDTO recordDTO) {
        //根据测试计划查询出来所有的记录
        Record record = recordConverter.recordDTOForRecord(recordDTO);
        System.out.println("根据测试计划查询出来所有的记录"+record);
        //Integer planId = recordDTO.getPlan_id();
        record.setStatus(1);
        List<Record> recordList = recordMapper.select(record);
        System.out.println("recordList:"+recordList);
        return recordConverter.recordListForRecordDTOList(recordList);
    }

    //@Override
    //public List<RecordDTO> getRecordDTOList(RecordDTO recordDTO) {
    //    return null;
    //}

    @Override
    public List<RecordDTO> getRecordList() {
        //获取所有的测试任务
        Record record1 = new Record();
        record1.setStatus(1);
        List<Record> recordList = recordMapper.select(record1);

        System.out.println(recordList);
        //如果测试执行为空则返回null
        if(Objects.isNull(recordList))
            return null;

        return recordConverter.recordListForRecordDTOList(recordList);
    }

    @Override
    public int updateReport(ReportDTO reportDTO) {
        String originalUrl = reportDTO.getBuild_url();
        System.out.println("originalUrl:"+originalUrl);
        // 使用正则表达式将字符串拆分
        String[] parts = originalUrl.split("/");

        // 获取拆分后的两部分
        String baseUrl = parts[0] + "//" + parts[2] + "/" + parts[3] + "/" + parts[4] + "/";
        String numberPart = parts[5];

        // 输出拆分后的部分
        System.out.println("基础 URL 部分: " + baseUrl);
        System.out.println("数字部分: " + numberPart);
        //获取具体的测试任务
        Record record1 = new Record();
        record1.setStatus(1);
        record1.setBuildUrl(baseUrl);
        record1.setJobId(numberPart);

        Record record = recordMapper.selectOne(record1);
        //更新allure报告地址
        record.setReportUrl(baseUrl+numberPart+"/allure/");
        record.setUpdateTime(new Date());
        int i = recordMapper.updateByPrimaryKeySelective(record);
        System.out.println("构建完成...");
        return i;
    }
}
